package com.example.myapplication.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.databinding.ItemIndicatorBinding

class IndicatorAdapter : ListAdapter<Pair<String, String>, IndicatorAdapter.IndicatorViewHolder>(IndicatorDiffCallback()) {
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IndicatorViewHolder {
        val binding = ItemIndicatorBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return IndicatorViewHolder(binding)
    }
    
    override fun onBindViewHolder(holder: IndicatorViewHolder, position: Int) {
        holder.bind(getItem(position))
    }
    
    class IndicatorViewHolder(
        private val binding: ItemIndicatorBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        
        fun bind(indicator: Pair<String, String>) {
            binding.indicatorNameTextView.text = indicator.first
            binding.indicatorValueTextView.text = indicator.second
        }
    }
    
    class IndicatorDiffCallback : DiffUtil.ItemCallback<Pair<String, String>>() {
        override fun areItemsTheSame(
            oldItem: Pair<String, String>,
            newItem: Pair<String, String>
        ): Boolean {
            return oldItem.first == newItem.first
        }
        
        override fun areContentsTheSame(
            oldItem: Pair<String, String>,
            newItem: Pair<String, String>
        ): Boolean {
            return oldItem == newItem
        }
    }
}

